/**
 * 
 */

import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import junit.framework.Assert;

import org.junit.Test;

/**
 * Program name: Module1
 * 
 * @author: Javier Panchi <javier.panchi@uqconnect.edu.au>
 * @version: 1.6
 * @since: 2011-09-28 Description: Test case for Module 1.
 */

public class Module1 {
	private static Value value = new Value();
	private static InputReader inputReader;
	private static Parser parser;
	private static final String fileName = "test.txt";
	private static final String regex = "^([a-zA-Z][:]([\\s]*[a-zA-Z0-9-+@#$%^&*|<>?]*[\\s]*[,])*)([\\s]*[a-zA-Z0-9-+@#$%^&*|<>?]*[\\s]*)$";

	/**
	 * Check the data of the file
	 */
	@Test
	public void checkFile() {
		try {
			FileReader reader = new FileReader(fileName);
			BufferedReader buffer = new BufferedReader(reader);
			String line = buffer.readLine();
			while (line != null) {
				if (!line.matches(regex)) {
					fail("Wrong value");
					break;
				}
				line = buffer.readLine();
			}
			reader.close();
		} catch (IOException ex) {
			ex.printStackTrace();
			fail("No such file");
		}
	}

	/**
	 * Check the file reader with the file testmodule1.txt
	 */
	@Test
	public void checkArrayListFile() {
		ArrayList<String> list = InputReader.readFile("testmodule1.txt");
		Assert.assertEquals("A:B,C,E", list.get(0));
		Assert.assertEquals("B:D", list.get(1));
		Assert.assertEquals("C:J", list.get(2));
	}

	/**
	 * Check a simple dependence
	 */
	@Test
	public void checkSizeAddDependence() {
		Value value = new Value();
		value.name = "T";
		Module1.value.addDependency(value);
		Assert.assertEquals(1, Module1.value.dependencies.size());
	}

	/**
	 * Check a complex dependence
	 */
	@Test
	public void checkSizeAddMultipleDependences() {
		Value value0 = new Value();
		Value value1 = new Value();
		Value value2 = new Value();
		Value value3 = new Value();
		Value value4 = new Value();
		Value value5 = new Value();
		Value value6 = new Value();
		Value value7 = new Value();
		Value value8 = new Value();
		Value value9 = new Value();
		Value value10 = new Value();
		Module1.value.addDependency(value);
		value2.addDependency(value3);
		value0.addDependency(value1);
		value0.addDependency(value2);
		value0.addDependency(value4);
		value5.addDependency(value6);
		value5.addDependency(value7);
		value9.addDependency(value10);
		Assert.assertEquals(3, value0.dependencies.size());
		Assert.assertEquals(2, value5.dependencies.size());
		Assert.assertEquals(0, value8.dependencies.size());
		Assert.assertEquals(1, value9.dependencies.size());
	}

	/**
	 * Check the names of the dependence
	 */
	@Test
	public void checkSizeAddMultipleDependencesNames() {
		Value value0 = new Value();
		value0.name = "0";
		Value value1 = new Value();
		value1.name = "1";
		Value value2 = new Value();
		value2.name = "2";
		Value value3 = new Value();
		value3.name = "3";
		Value value4 = new Value();
		value4.name = "4";
		Value value5 = new Value();
		value5.name = "5";
		Value value6 = new Value();
		value6.name = "6";
		Value value7 = new Value();
		value7.name = "7";
		Value value8 = new Value();
		value8.name = "8";
		Value value9 = new Value();
		value9.name = "9";
		Value value10 = new Value();
		value10.name = "10";
		Module1.value.addDependency(value);
		value2.addDependency(value3);
		value0.addDependency(value1);
		value0.addDependency(value2);
		value0.addDependency(value4);
		value5.addDependency(value6);
		value5.addDependency(value7);
		value9.addDependency(value10);
		Assert.assertEquals("3",
				value0.dependencies.get(1).dependencies.get(0).name.toString());
		Assert.assertEquals("7", value5.dependencies.get(1).getName());
		Assert.assertEquals("8", value8.name);
		Assert.assertEquals("10", value9.dependencies.get(0).name);
	}
}
